using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public struct VTouch
{
	public Vector2 	position;
	public int 		ID;
}

public struct VPattern
{
	public int 		userData;
	public Vector2	position;		
	public Vector2	direction;
	public int		touchFrontID;	
	public int		touchLeftID;	
	public int		touchRightID;
}
public class VPatternDetector 
{	
#if (UNITY_ANDROID || UNITY_IPHONE) && ! UNITY_EDITOR
	static VPatternDetector()
	{
		VPatternDetectorSetLicense(@"name:Master MAPI MAJE
appID: com.MasterMaje.Intrinity
expires: 32/12/2016
license: 4B3A2934F3CF3297396BF6B5D6C0BE642495B816ABFCF53860E21D57BA3D 0CFEF016231DEF3BB1AD5F4F93FDE06A70B3461C0AD62036E92C44EB7CA5 8C22CA0B1E61C863FC2A9EDE04E2CAB7BCF61F5EFB2AB5C31C06EE94957C ACDF643D17281393F10FB9F76B2B08FA7EE8F3E89DB9FB8D883F13AD3A88 DA58B9A61408838E ");
				
#if UNITY_IPHONE
		VPatternDetectorSetPPIForViewport(Screen.width,Screen.height);
#else
		VPatternDetectorSetScreenPPI(Screen.dpi);
#endif
	}

#endif
	
	// public API
	public static bool IsSupported()
	{
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID)
		return true;
#else
		return false;
#endif
	}
	
	public static float GetScreenPPI()
	{
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID)
		return VPatternDetectorGetScreenPPI();
#else
		return 0;
#endif
	}
		
	public static float GetMM2PX()
	{
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID)
		return VPatternDetectorGetMM2PX();
#else
		return 0;
#endif
	}

	public static void SetDistanceTolerance(float iTolerance)
	{
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID)
		VPatternDetectorSetDistanceTolerance(iTolerance);
#endif	
	}

	public static float GetDistanceTolerance()
	{
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID)
		return VPatternDetectorGetDistanceTolerance();
#else
		return 0;
#endif
	}

	public static void SetSmoothing(float iSmoothing)
	{
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID)
		VPatternDetectorSetSmoothing(iSmoothing);
#endif			
	}

	public static float GetSmoothing()
	{
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID)
		return VPatternDetectorGetSmoothing();
#else
		return 0;
#endif
	}
	
	public static bool RegisterPattern(float iDistance, Vector2 iPosition, int iUserData, Vector2 iOffset)
	{
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID)
		return VPatternDetectorRegisterPattern(iDistance, iPosition, iUserData, iOffset);
#else
		return false;
#endif
	}

	public static bool UnregisterPattern(int iUserData)
	{
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID)
		return VPatternDetectorUnregisterPattern(iUserData);
#else
		return false;
#endif
	}

	public static void UnregisterPatterns()
	{
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID)
		VPatternDetectorUnregisterPatterns();
#endif
	}

	public static int DetectPatterns()
	{
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID)
		int touchCount 		= Input.touchCount;
		VTouch[] vTouches 	= new VTouch[touchCount];
		int realTouchCount 	= 0;
		
		for (int i = 0; i < touchCount; ++i) {
			Touch touch = Input.GetTouch(i);	
			if (touch.phase!=TouchPhase.Ended && touch.phase!=TouchPhase.Canceled) {
				vTouches[realTouchCount].position.x = touch.position.x;
				vTouches[realTouchCount].position.y = touch.position.y;
				vTouches[realTouchCount].ID = touch.fingerId;
				realTouchCount++;
			}
		}
		
		return VPatternDetectorDetectPatterns(vTouches, realTouchCount);
#else
		return 0;
#endif
	}
	
	public static int GetDetectedPatternCount()
	{
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID)
		return VPatternDetectorGetDetectedPatternCount();
#else
		return 0;
#endif
	}

	public static VPattern GetDetectedPattern(int iIndex)
	{
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID)
		return VPatternDetectorGetDetectedPattern(iIndex);
#else
		VPattern pat = new VPattern();
		return pat;
#endif
	}
	

	
#if !UNITY_EDITOR

	
	// C API calls
#if UNITY_IPHONE
	[DllImport ("__Internal")]
	private static extern void VPatternDetectorSetScreenPPI(float iPPI);
	
	[DllImport ("__Internal")]
	private static extern float VPatternDetectorDetectScreen();
	
	[DllImport ("__Internal")]
	private static extern float VPatternDetectorGetScreenPPI();

	[DllImport ("__Internal")]
	private static extern float VPatternDetectorGetMM2PX();

	[DllImport ("__Internal")]
	private static extern void VPatternDetectorSetDistanceTolerance(float iTolerance);
	
	[DllImport ("__Internal")]
	private static extern float VPatternDetectorGetDistanceTolerance();
	
	[DllImport ("__Internal")]
	private static extern void VPatternDetectorSetSmoothing(float iSmoothing);
	
	[DllImport ("__Internal")]
	private static extern float VPatternDetectorGetSmoothing();

	[DllImport ("__Internal")]
	private static extern bool VPatternDetectorRegisterPattern(float iDistance, Vector2 iPosition, int iUserData, Vector2 iOffset);
	
	[DllImport ("__Internal")]
	private static extern bool VPatternDetectorUnregisterPattern(int iUserData);

	[DllImport ("__Internal")]
	private static extern void VPatternDetectorUnregisterPatterns();
	
	[DllImport ("__Internal")]
	private static extern int VPatternDetectorDetectPatterns(VTouch[] iTouches, int iTouchCount);
	
	[DllImport ("__Internal")]
	private static extern int VPatternDetectorGetDetectedPatternCount();	
	
	[DllImport ("__Internal")]
	private static extern VPattern VPatternDetectorGetDetectedPattern(int iIndex);	
	
	[DllImport ("__Internal")]
	private static extern void VPatternDetectorSetLicense(string license);

	[DllImport ("__Internal")]
	private static extern void VPatternDetectorSetPPIForViewport(int width,int height);
	
	
#elif UNITY_ANDROID
	[DllImport ("VolumiquePatternDetector")]
	private static extern void VPatternDetectorSetScreenPPI(float iPPI);

	[DllImport ("VolumiquePatternDetector")]
	private static extern float VPatternDetectorGetScreenPPI();

	[DllImport ("VolumiquePatternDetector")]
	private static extern float VPatternDetectorGetMM2PX();

	[DllImport ("VolumiquePatternDetector")]
	private static extern void VPatternDetectorSetDistanceTolerance(float iTolerance);

	[DllImport ("VolumiquePatternDetector")]
	private static extern float VPatternDetectorGetDistanceTolerance();

	[DllImport ("VolumiquePatternDetector")]
	private static extern void VPatternDetectorSetSmoothing(float iSmoothing);

	[DllImport ("VolumiquePatternDetector")]
	private static extern float VPatternDetectorGetSmoothing();

	[DllImport ("VolumiquePatternDetector")]
	private static extern bool VPatternDetectorRegisterPattern(float iDistance, Vector2 iPosition, int iUserData, Vector2 iOffset);

	[DllImport ("VolumiquePatternDetector")]
	private static extern bool VPatternDetectorUnregisterPattern(int iUserData);

	[DllImport ("VolumiquePatternDetector")]
	private static extern void VPatternDetectorUnregisterPatterns();

	[DllImport ("VolumiquePatternDetector")]
	private static extern int VPatternDetectorDetectPatterns(VTouch[] iTouches, int iTouchCount);

	[DllImport ("VolumiquePatternDetector")]
	private static extern int VPatternDetectorGetDetectedPatternCount();
	

	[DllImport ("VolumiquePatternDetector")]
	private static extern VPattern VPatternDetectorGetDetectedPattern(int iIndex);
	
	[DllImport ("VolumiquePatternDetector")]
	private static extern void VPatternDetectorSetLicense(string license);
	
#endif
	
	
#endif
}
